package com.example.boom.model;

public class Instruction {
    private String musicName;   // 歌曲名称
    private int volume = -1;    // 音量
    private int num = -1;       // 重复次数
    private boolean playCMD;    // 播放指令
    private boolean stopCMD;    // 暂停指令
    private boolean statusCMD;  // 状态讯问指令

    /***
     * 构造控制指令
     * @param message
     * message个格式:
     *  设置指令
     *      播放1.mp3 音量100 循环4次
     *      m 1 v 100 n 4
     *
     *  暂停指令
     *      stop
     *
     *  播放指令
     *      start
     *
     *  状态查询命令
     *      status
     */
    public Instruction(String message) {
        if (message.equals("status")) {
            statusCMD = true;
            return;
        }
        if (message.equals("stop")) {
            stopCMD = true;
            return;
        }
        if (message.equals("start")) {
            playCMD = true;
            return;
        }
        String[] fields = message.split(" ");
        for (int i = 0; i < fields.length && i + 1 < fields.length; i = i + 2) {
            HandelCMD(fields[i], fields[i + 1]);
        }
    }

    private void HandelCMD(String v1, String v2) {
        switch (v1) {
            case "m":
                this.musicName = v2;
                return;
            case "v":
                this.volume = Integer.parseInt(v2);
                return;
            case "n":
                this.num = Integer.parseInt(v2);
                return;
            default:
        }
    }

    public String getMusicName() {
        return musicName + ".mp3";
    }

    public int getVolume() {
        return volume;
    }

    public int getNum() {
        return num;
    }

    public boolean isPlayCMD() {
        return playCMD;
    }

    public boolean isStopCMD() {
        return stopCMD;
    }

    public boolean isStatusCMD() {
        return statusCMD;
    }

    @Override
    public String toString() {
        return "Instruction{" +
                "musicName='" + musicName + '\'' +
                ", volume=" + volume +
                ", num=" + num +
                ", playCMD=" + playCMD +
                ", stopCMD=" + stopCMD +
                ", statusCMD=" + statusCMD +
                '}';
    }
}
